<template>
  <div class="register-container">
    <div class="register-card">
      <div class="register-header">
        <h2>用户注册</h2>
        <p>创建您的账户，开始使用一站式接口平台</p>
      </div>
      
      <a-form
        :model="registerForm"
        :rules="rules"
        @finish="handleRegister"
        class="register-form"
      >
        <a-form-item name="userAccount">
          <a-input
            v-model:value="registerForm.userAccount"
            placeholder="请输入账号"
            size="large"
          >
            <template #prefix>
              <UserOutlined />
            </template>
          </a-input>
        </a-form-item>
        
        <a-form-item name="userPassword">
          <a-input-password
            v-model:value="registerForm.userPassword"
            placeholder="请输入密码"
            size="large"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>
        
        <a-form-item name="checkPassword">
          <a-input-password
            v-model:value="registerForm.checkPassword"
            placeholder="请确认密码"
            size="large"
          >
            <template #prefix>
              <LockOutlined />
            </template>
          </a-input-password>
        </a-form-item>
        
        <a-form-item>
          <a-button
            type="primary"
            html-type="submit"
            size="large"
            :loading="loading"
            block
          >
            注册
          </a-button>
        </a-form-item>
      </a-form>
      
      <div class="register-footer">
        <a-button type="link" @click="goToLogin">
          已有账号？立即登录
        </a-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { UserOutlined, LockOutlined } from '@ant-design/icons-vue'
import { message } from 'ant-design-vue'
import { userRegisterUsingPost } from '@/api/userController'

// 路由
const router = useRouter()

// 响应式数据
const loading = ref(false)
const registerForm = reactive({
  userAccount: '',
  userPassword: '',
  checkPassword: ''
})

// 表单验证规则
const rules = {
  userAccount: [
    { required: true, message: '请输入账号', trigger: 'blur' },
    { min: 4, message: '账号至少4个字符', trigger: 'blur' },
    { max: 20, message: '账号最多20个字符', trigger: 'blur' },
    { 
      pattern: /^[a-zA-Z0-9_]+$/, 
      message: '账号只能包含字母、数字和下划线', 
      trigger: 'blur' 
    }
  ],
  userPassword: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 8, message: '密码至少8个字符', trigger: 'blur' },
    { max: 20, message: '密码最多20个字符', trigger: 'blur' },
    { 
      pattern: /^(?=.*[a-zA-Z])(?=.*\d)/, 
      message: '密码必须包含字母和数字', 
      trigger: 'blur' 
    }
  ],
  checkPassword: [
    { required: true, message: '请确认密码', trigger: 'blur' },
    {
      validator: (rule, value) => {
        if (value !== registerForm.userPassword) {
          return Promise.reject('两次输入的密码不一致')
        }
        return Promise.resolve()
      },
      trigger: 'blur'
    }
  ]
}

// 注册处理
const handleRegister = async () => {
  try {
    loading.value = true
    
    // 准备注册数据
    const registerData = {
      userAccount: registerForm.userAccount,
      userPassword: registerForm.userPassword,
      checkPassword: registerForm.checkPassword
    }
    
    const response = await userRegisterUsingPost(registerData)
    
    message.success('注册成功！请登录')
    
    // 跳转到登录页面
    router.push('/login')
    
  } catch (error) {
    console.error('注册失败：', error)
    message.error(error.message || '注册失败，请检查输入信息')
  } finally {
    loading.value = false
  }
}

// 跳转到登录页面
const goToLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  padding: 20px;
}

.register-card {
  background: white;
  border-radius: 12px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
}

.register-header {
  text-align: center;
  margin-bottom: 32px;
}

.register-header h2 {
  color: #333;
  margin-bottom: 8px;
  font-size: 24px;
  font-weight: 600;
}

.register-header p {
  color: #666;
  margin: 0;
  font-size: 14px;
}

.register-form {
  margin-bottom: 24px;
}

.register-footer {
  text-align: center;
}

.register-footer .ant-btn-link {
  color: #1890ff;
  padding: 0;
}

.register-footer .ant-btn-link:hover {
  color: #40a9ff;
}

/* 响应式设计 */
@media (max-width: 480px) {
  .register-container {
    padding: 16px;
  }
  
  .register-card {
    padding: 24px;
  }
  
  .register-header h2 {
    font-size: 20px;
  }
}

/* 密码强度提示样式 */
:deep(.ant-form-item-explain-error) {
  font-size: 12px;
}

/* 输入框样式优化 */
:deep(.ant-input-affix-wrapper) {
  border-radius: 6px;
}

:deep(.ant-input-affix-wrapper:hover) {
  border-color: #40a9ff;
}

:deep(.ant-input-affix-wrapper-focused) {
  border-color: #1890ff;
  box-shadow: 0 0 0 2px rgba(24, 144, 255, 0.2);
}

/* 按钮样式优化 */
:deep(.ant-btn-primary) {
  border-radius: 6px;
  height: 40px;
  font-size: 16px;
  font-weight: 500;
}

:deep(.ant-btn-primary:hover) {
  background: #40a9ff;
  border-color: #40a9ff;
}
</style>
